<?php
/**
* PHP Template.
*/
class Login extends Model {
    public $LOGIN_INVALID_LOGIN_COMBO = -1;
    public $LOGIN_INVALID_LOGINNAME = 0;
    public $LOGIN_SUCCESS = 1;

    public function __construct(){
            parent::Model();
        }
    public function isValidLogin($username,$passwd){
            $retVal = $this->LOGIN_SUCCESS;
            $queryResultObj = $this->db->query("SELECT username FROM users WHERE username=? AND passwd=?",array($username,$passwd));
            $tmpVal = ($queryResultObj->num_rows() > 0);
            $queryResultObj->free_result();
            if(!$tmpVal){
                    $queryResultObj = $this->db->query("SELECT username FROM users WHERE username=? AND passwd=?",array($username,$passwd));
                    $retVal = ($queryResultObj->num_rows() > 0);
                    $queryResultObj->free_result();
                    if($tmpVal) {
                            $retVal = $this->LOGIN_INVALID_LOGINNAME; // probably not going to happen, but just in case
                        } else {
                            $retVal = $this->LOGIN_INVALID_LOGIN_COMBO;
                        }
                }
            return $retVal;
        }
    public function getUsername($p_id){
            $rs = $this->db->query("SELECT username FROM users WHERE p_id=?", array($p_id));
            if($rs->num_rows()>0){
                    return $rs->row()->username;
                } else {
                    return "";
                }
        }
    public function createUser($pid,$username,$password){
            $this->setLogin($pid,$username,$password);
        }
    public function setLogin($pid,$username,$password){
        // check to see if username exist?
            $data = array(
                'p_id' => $pid ,
                'username' => $username ,
                'passwd' => $password
                );
            $this->db->insert('users', $data);
        }
}
?>